#! /bin/sh -vx
# $Id$
# Copyright 2017-2022 Karl Berry <tex-live@tug.org>
# Copyright 2013 Peter Breitenlohner <tex-live@tug.org>
# You may freely use, modify and/or distribute this file.

BinDir=${BinDir:-.}
ExeExt=${ExeExt:-}
_mendex=$BinDir/mendex$ExeExt

TEXMFCNF=$srcdir/../kpathsea
export TEXMFCNF

rm -f foo.* uni.* range*.* pprec*.* romalp*.* sjis.* euc.* enc*.*
rc=0

$_mendex $srcdir/tests/foo.idx -o foo.ind1 -t foo.ilg1 \
	&& diff $srcdir/tests/foo.ind foo.ind1 || rc=1

cat $srcdir/tests/foo.idx | $_mendex >foo.ind2 2>foo.ilg2 \
	&& diff $srcdir/tests/foo.ind foo.ind2 || rc=2

$_mendex -U -d $srcdir/tests/uni.dict -s $srcdir/tests/uni.ist \
	$srcdir/tests/uni.idx -o uni.ind1 -t uni.ilg1 \
	&& diff $srcdir/tests/uni.ind uni.ind1 || rc=3

cat $srcdir/tests/uni.idx | \
	$_mendex -U -d $srcdir/tests/uni.dict -s $srcdir/tests/uni.ist \
	>uni.ind2 2>uni.ilg2 \
	&& diff $srcdir/tests/uni.ind uni.ind2 || rc=4

$_mendex -U -d $srcdir/tests/uni.dict -s $srcdir/tests/uni1.ist -s $srcdir/tests/uni2.ist \
	$srcdir/tests/uni.idx -o uni.ind3 -t uni.ilg3 \
	&& diff $srcdir/tests/uni.ind uni.ind3 || rc=5

cat $srcdir/tests/uni.idx | \
	guess_input_kanji_encoding=1 $_mendex -U -d $srcdir/tests/uni.dict -s $srcdir/tests/sjis.ist \
	>sjis.ind1 2>sjis.ilg1 \
	&& diff $srcdir/tests/uni.ind sjis.ind1 || rc=20

cat $srcdir/tests/uni.idx | \
	$_mendex -U -d $srcdir/tests/uni.dict -s $srcdir/tests/euc.ist --guess-input-enc \
	>euc.ind1 2>euc.ilg1 \
	&& diff $srcdir/tests/uni.ind euc.ind1 || rc=21


# test for range suffix_2p, suffix_3p, suffix_mp
for num in 1 2 3; do
$_mendex -s $srcdir/tests/range$num.ist $srcdir/tests/range.idx \
	-o range$num.ind1 -t range$num.ilg \
	&& diff $srcdir/tests/range$num.ind range$num.ind1 || rc=6
done


# test for page_precedence and suffix_3p
sfx=A
for num in 0 1 2; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/pprec$sfx.idx \
	-o pprec$sfx-$num.ind1 -t pprec$sfx-$num.ilg \
	&& diff $srcdir/tests/pprec$sfx-$num.ind pprec$sfx-$num.ind1 || rc=7
done
sfx=B
for num in 0 3 4; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/pprec$sfx.idx \
	-o pprec$sfx-$num.ind1 -t pprec$sfx-$num.ilg \
	&& diff $srcdir/tests/pprec$sfx-$num.ind pprec$sfx-$num.ind1 || rc=8
done


# test for page_precedence, heuristic detection if a letter is Roman or Alpha
sfx=A
for num in 5 6; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/romalp$sfx.idx \
	-o romalp$sfx-$num.ind1 -t romalp$sfx-$num.ilg \
	&& diff $srcdir/tests/romalp$sfx-$num.ind romalp$sfx-$num.ind1 || rc=9
done
sfx=B
for num in 5 6 7; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/romalp$sfx.idx \
	-o romalp$sfx-$num.ind1 -t romalp$sfx-$num.ilg \
	&& diff $srcdir/tests/romalp$sfx-$num.ind romalp$sfx-$num.ind1 || rc=10
done
sfx=C
for num in 5; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/romalp$sfx.idx \
	-o romalp$sfx-$num.ind1 -t romalp$sfx-$num.ilg \
	&& diff $srcdir/tests/romalp$sfx-$num.ind romalp$sfx-$num.ind1 || rc=11
done
sfx=D
for num in 5 6 7; do
$_mendex -s $srcdir/tests/pprec$num.ist $srcdir/tests/romalp$sfx.idx \
	-o romalp$sfx-$num.ind1 -t romalp$sfx-$num.ilg \
	&& diff $srcdir/tests/romalp$sfx-$num.ind romalp$sfx-$num.ind1 || rc=12
done

# guess file encoding
# input from file
for enc in j e s u; do
$_mendex --guess-input-enc -s $srcdir/tests/jis.ist -d $srcdir/tests/enc.dic $srcdir/tests/enc-$enc.idx \
	-o enc-$enc.ind1 -t enc-$enc.ilg \
	&& diff $srcdir/tests/enc-x.ind enc-$enc.ind1 || rc=101
done

for enc in j u; do
$_mendex --no-guess-input-enc -s $srcdir/tests/uni.ist -d $srcdir/tests/enc.dic $srcdir/tests/enc-$enc.idx \
	-o enc-$enc.ind1 -t enc-$enc.ilg \
	&& diff $srcdir/tests/enc-x.ind enc-$enc.ind1 || rc=102
done

# input from pipe
for enc in j u; do
cat $srcdir/tests/enc-$enc.idx | \
	$_mendex --guess-input-enc -i -s $srcdir/tests/euc.ist -d $srcdir/tests/enc.dic \
	-o enc-p-$enc.ind1 -t enc-p-$enc.ilg \
	&& diff $srcdir/tests/enc-x.ind enc-p-$enc.ind1 || rc=103
done

# imput from redirect
for enc in j u; do
$_mendex --guess-input-enc -i -s $srcdir/tests/sjis.ist -d $srcdir/tests/enc.dic < $srcdir/tests/enc-$enc.idx \
	-o enc-r-$enc.ind1 -t enc-r-$enc.ilg \
	&& diff $srcdir/tests/enc-x.ind enc-r-$enc.ind1 || rc=104
done


exit $rc
